// pages/edit/edit.js
var app=getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		array: [ '供给', '需求'], //类型
		index: 0,//行业数组索引,
    items: [{ name: '1', value: '是否发布到市场', checked: 'true' }],
    display:'block',
    is_show:1,
    info_id:'',
    src1:'',
    image:[]
	},


  checkboxChange: function (e) {
    console.log(e)
    if (e.detail.value[0]==1){
        this.setData({
          is_show:1
        })
    }else{
      this.setData({
        is_show: 0
      })
    }
    console.log('checkbox发生change事件，携带value值为：', e.detail.value[0])
  },


	addnine: function (e) {
    var that=this
		wx.chooseImage({
			count: 3, // 可以同时选择的图片数量
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: function (res) {
				// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
				var tempFilePaths = res.tempFilePaths;
        that.setData({
          src: tempFilePaths
        })
        wx.uploadFile({
          url: app.globalData.url +'xcx/UpresourceImg',
          filePath: tempFilePaths[0],
          name: 'fileimg',
          formData: {

            'isimg': 1
          },
          success:function(r){
              console.log(r.data)
              console.log(r)
              var image = that.data.image

              image.push(r.data)
              that.setData({
                  src1:r.data,
                  display: 'none',
                  image:image
              })

              wx.uploadFile({
                url: app.globalData.url +'xcx/UpresourceImg',
                filePath: tempFilePaths[1],
                name: 'fileimg',
                formData: {

                  'isimg': 1
                },
                success: function (r1) {
                  console.log(r1.data)
                  console.log(r1)
                  var image = that.data.image
                  image.push(r1.data)
                  that.setData({
                    src2: r1.data,
                    display: 'none',
                    image:image
                  })


                  wx.uploadFile({
                    url: app.globalData.url +'xcx/UpresourceImg',
                    filePath: tempFilePaths[2],
                    name: 'fileimg',
                    formData: {

                      'isimg': 1
                    },
                    success: function (r2) {
                      console.log(r2.data)
                      console.log(r2)
                      var image = that.data.image
                       image.push(r2.data)

                        that.setData({
                          src3: r2.data,
                          display: 'none',
                          image:image
                        })



                     
                    }
                  })
                }
              })
          }

        


        })



      }



		})

	},
	
  delimg:function(e){
    console.log(e.currentTarget.dataset.index)
    var index = e.currentTarget.dataset.index
    var imgurl = e.currentTarget.id
    var that = this
    var src1 = that.data.image
    var info_id = that.data.info_id
    // var imgid=''
    var imgone=this.data.src1
    // imgName: imgurl
    console.log(src1)
    if (imgurl) {
      src1.splice(index, 1);
      that.setData({
        image: src1
      })
    }
      // if(index==0){
      //  imgid= 'imgone'
      // }
      // if(index==1){
      //   imgid = 'imgtwo'
      // }
      // if(index==2){
      //   imgid = 'imgthree'
      // }
      // console.log(imgid)
      // wx.request({
      //   url: app.globalData.url + 'xcx/deleteImg',
      //   data: { info_id: info_id, imgid:imgid,imgone:imgone },
      //   method: "post",
      //   success: function (res) {
      //     console.log(res)
      //     if (res.data.code == 10000) {
      //       wx.showToast({
      //         title: '删除成功',
      //         icon: 'none'
      //       })
      //       src1.splice(index, 1);
      //       that.setData({
      //         image: src1
      //       })
      //     } else {
            // wx.showToast({
            //   title: '删除失败',
            //   icon: 'none'
            // })
          //   src1.splice(index, 1);
          //   that.setData({
          //     image: src1
          //   })
          // }





      //   }
      // })

    // } 
  },
	//行业选择点击时改变view的显示
	bindPickerChange: function (e) {
		this.setData({
			index: e.detail.value
		})
    console.log(this.data.array[this.data.index])
	},


  formSubmit:function(e){
    console.log(e)
    var warn = "";//弹框时提示的内容  
    var flag = true;//判断信息输入是否完整  
    if (e.detail.value.title == "") {
      warn = "请填写标题！";
    } else if (e.detail.value.title.length>20){
      warn = "标题不得超过20个字！";
    }
    else if (e.detail.value.keyword1.length>9){
      warn = "关键词一不得超过9个字！";
    }
    else if (e.detail.value.keyword1 == "") {
      warn = "请填写关键词一！";
    } else if (e.detail.value.keyword2.length >9){
      warn = "关键词二不得超过9个字！";
    }
     else if (e.detail.value.keyword2 == '') {
      warn = '请填写关键词二！'
    } else if (e.detail.value.keyword3.length>9){
      warn = "关键词三不得超过9个字！";
    }
    else if (e.detail.value.keyword3 == '') {
      warn = '请填写关键词三！'
    } else if (this.data.image == '') {
      warn = '请选择图片！'
    }
    else if (e.detail.value.pro == '') {
      warn = "请填写产品描述！";
    } else {
      var that=this
      var flag = false;
      var wx_id=wx.getStorageSync('uid');
      var texttype=that.data.array[that.data.index]
     
    
      if (texttype=='供给'){
        that.setData({
          info_type:0
        })
      }else{
        that.setData({
          info_type: 1
        })
      }
      console.log(wx_id)
      wx.request({
        url: app.globalData.url +'xcx/addresource',
        method: "post",
        data:{
          user_wx_id:wx_id,
          info_id:that.data.info_id,
          info_type:that.data.info_type,
          title: e.detail.value.title,
          keywords: e.detail.value.keyword1 + ',' + e.detail.value.keyword2 + ',' + e.detail.value.keyword3,
          info_desc: e.detail.value.pro,
          is_show:that.data.is_show,
          // imgone:that.data.src1,
          // imgtwo: that.data.src2,
          // imgthree: that.data.src3
          imgs:that.data.image[0]+','+that.data.image[1]+','+that.data.image[2]
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success:function(res){
          console.log(res)
          if(res.data.code=='10000'){
            
            wx.showToast({
              title: '成功',
              icon: 'success',
              duration: 1000
            })
            wx.redirectTo({
              url: '../gx/gx'
            })
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 1000
            })
          }
         


        }
      })


    }
  
    if (flag == true) {
      wx.showToast({
        title: warn,
        icon: 'none',
        duration: 1000
      })
    }  


  },


	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
   
       var id=options.id;
       var that=this;
       var wx_id=wx.getStorageSync('uid')
       console.log(id)
       if(id){
         wx.request({
           url: app.globalData.url +'xcx/detialresource/' + id+'/'+wx_id,
           data: {},
           method: "get",
           success: function (res) {
             console.log(res)
              var arr=res.data.data;
              var key=arr.keywords;
              var key = key.split(",");
              console.log(key)
              var image = []
              if (arr.imgone != 'undefined') {
                image.push(arr.imgone)
              }
              if (arr.imgtwo != 'undefined') {
                image.push(arr.imgtwo)
              }
              if (arr.imgthree !='undefined') {
                image.push(arr.imgthree)
              }
              if(arr){
                that.setData({
                  info_id:id,
                  index:arr.info_type,
                  title:arr.title,
                  key1:key[0],
                  key2:key[1],
                  key3:key[2],
                  text:arr.info_desc,
                  // src1:arr.imgone,
                  // src2:arr.imgtwo,
                  // src3:arr.imgthree,
                  image: image
                })
               
              


              if(arr.is_show==1){
                that.setData({
                  items: [{ name: '1', value: '是否发布到市场', checked: 'true' }],
                  is_show: 1
                })
              }else{
                that.setData({
                  items: [{ name: '1', value: '是否发布到市场' }],
                  is_show:0
                })
              }



              }
            console.log(that.data)


           }
         })
         console.log(that.data)
       }
      
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})



